
#include "LNotifyMgr.h"
#include "LogicService.h"
#include "Character/CreatureMgr.h"
#include "Common/TableDataEx/ChatCfg.h"
#include "Common/ChatDefine.h"
#include "Chat/ChatManager.h"
LNotifyMgr::LNotifyMgr()
{
}

LNotifyMgr::~LNotifyMgr()
{
}

bool LNotifyMgr::Init()
{
	g_GetEvent()->Subscribe(this,EVENT_EQUIP_REFINE, 0, CREATURE_PLAYER, "LNotifyMgr");
	g_GetEvent()->Subscribe(this, EVENT_COMPOSE, 0, CREATURE_PLAYER, "LNotifyMgr");
	g_GetEvent()->Subscribe(this, EVENT_ITEM_CHANGE, 0, CREATURE_PLAYER, "LNotifyMgr");	
	
	return true;
}

void LNotifyMgr::UnInit()
{
	g_GetEvent()->UnSubscribeAll(this);
}

void LNotifyMgr::OnExecute(uint16_t nEventID, uint64_t nSrcID, uint8_t bySrcType, void *pContext, int32_t nLen)
{
	switch (nEventID)
	{
	case EVENT_EQUIP_REFINE:
		{
			EquipRefineEvent refineEvent = *(EquipRefineEvent*)pContext;
			OnEquipRefine(nSrcID, refineEvent);
		}
		break;
	case EVENT_COMPOSE:
		{
			ComposeEvent composeEvent = *(ComposeEvent*)pContext;
			OnComposeMaterial(nSrcID, composeEvent);
		}
		break;
	case EVENT_ITEM_CHANGE:
		{
			ItemChangeEvent itemEvent = *(ItemChangeEvent*)pContext;
			OnItemChange(nSrcID, itemEvent);
		}
		break;
	}
}


//精炼
void LNotifyMgr::OnEquipRefine(CharIDType cid, EquipRefineEvent &refineEvent)
{
	if (!refineEvent.result)
	{
		return;
	}
	int32_t chatId = g_GetChatCfgMgr()->ChatIDByEventType(EChatSpecialEvent_EquipRefine, refineEvent.refinLevel);
	if (chatId <= 0)
	{
		return;
	}
	const EquipEquipCfgInfo *pEquipCfg = g_GetEquipEquipCfgTable()->GetEquipEquipCfgInfo(refineEvent.itemId);
	if (nullptr == pEquipCfg)
	{
		LogErrFmtPrint("LNotifyMgr::OnEquipRefine....nullptr == pEquipCfg..cid:%lu, itemid:%u ", cid, refineEvent.itemId);
		return;
	}
	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("LNotifyMgr::OnEquipRefine....nullptr == pPlayer..cid:%lu, itemid:%u ", cid, refineEvent.itemId);
		return;
	}
	
	SystemChatMsgData msgData;
	msgData.text.push_back(pPlayer->GetName());
	msgData.params.push_back(refineEvent.itemId);
	msgData.params.push_back(refineEvent.refinLevel);
	g_GetChatMgr()->SendG2WBroadcast((BROADCAST_TYPE)chatId, msgData, 0);

}
//合成材料
void LNotifyMgr::OnComposeMaterial(CharIDType cid, ComposeEvent &composeEvent)
{
	if (composeEvent.type != 1)//不是宝石合成
	{
		return;
	}
	const ItemItemCfgInfo *pItemCfg = g_GetItemItemCfgTable()->GetItemItemCfgInfo(composeEvent.itemId);
	if (nullptr == pItemCfg)
	{
		LogErrFmtPrint("LNotifyMgr::OnComposeMaterial....nullptr == pItemCfg..cid:%lu, itemid:%u ", cid, composeEvent.itemId);
		return;
	}
	int32_t chatId = g_GetChatCfgMgr()->ChatIDByEventType(EChatSpecialEvent_ComposStone, pItemCfg->itemLevel);
	if (chatId <= 0)
	{
		return;
	}
	
	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("LNotifyMgr::OnComposeMaterial....nullptr == pPlayer..cid:%lu, itemid:%u ", cid, composeEvent.itemId);
		return;
	}
	SystemChatMsgData msgData;
	msgData.text.push_back(pPlayer->GetName());
	msgData.params.push_back(composeEvent.num);
	msgData.params.push_back(composeEvent.itemId);
	g_GetChatMgr()->SendG2WBroadcast((BROADCAST_TYPE)chatId, msgData, 0);
}

//物品改变事件
void LNotifyMgr::OnItemChange(CharIDType cid, ItemChangeEvent &itemEvent)
{
	if (ENotifyType_Item_KillWildMons == itemEvent.itemSource.notifySource)
	{
		//暂时不处理
		return;
	}
	if (EItemOpetateType_Add != itemEvent.opetateType)
	{
		return;
	}
	int32_t chatId = g_GetChatCfgMgr()->ChatIDByEventType(itemEvent.itemSource.notifySource, itemEvent.itemId);
	if (chatId <= 0)
	{
		return;
	}
	Player *pPlayer = g_GetCreatureMgr()->GetPlayer(cid);
	if (nullptr == pPlayer)
	{
		LogErrFmtPrint("LNotifyMgr::OnItemChange....nullptr == pPlayer..cid:%lu,source:%d, itemid:%u ", cid, itemEvent.itemSource.notifySource, itemEvent.itemId);
		return;
	}

	SystemChatMsgData msgData;
	msgData.text.push_back(pPlayer->GetName());
	
	if (ENotifyType_Item_WorldBoss == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_NolinTreasure == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_SonOfKilling == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_MonsAttackCity == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_Cloud == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_WorldAnswer == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_LostCity == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_ChaosDragon == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_UnionEscort == itemEvent.itemSource.notifySource
		)
	{
		const ActivityDailyCfgInfo *pActivityCfg = g_GetActivityDailyCfgTable()->GetActivityDailyCfgInfo(itemEvent.itemSource.notifyVal);
		if (nullptr == pActivityCfg)
		{
			LogErrFmtPrint("LNotifyMgr::OnItemChange....nullptr == pActivityCfg..cid:%lu,source:%d,notifyval:%lu itemid:%u ", cid, itemEvent.itemSource.notifySource, itemEvent.itemId, itemEvent.itemSource.notifyVal);
			return;
		}
		msgData.params.push_back(pActivityCfg->activeId);
		msgData.params.push_back(itemEvent.itemNum);
		msgData.params.push_back(itemEvent.itemId);
	}
	else if (ENotifyType_Item_LukyTurntable == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_ScoreLottery == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_Treasure == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_LoopMission == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_StoryDup == itemEvent.itemSource.notifySource
		|| ENotifyType_Item_LadderDup == itemEvent.itemSource.notifySource
		)
	{
		msgData.params.push_back(itemEvent.itemNum);
		msgData.params.push_back(itemEvent.itemId);
	}
	g_GetChatMgr()->SendG2WBroadcast((BROADCAST_TYPE)chatId, msgData, 0);
}